package com.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.Urlconfig;
import com.base.po.voip.UrlconfigExample;
import com.base.po.voip.Users;
import com.base.po.voip.UsersExample;
import com.base.service.UrlconfigService;
import com.base.service.UsersService;
import com.base.utils.PinyinTool;
import com.base.utils.Produce;
import com.base.utils.PinyinTool.Type;

/**
 * 下载链接
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/download")
public class wap_download {
	@Autowired
	private UsersService usersService;
	@Autowired
	private UrlconfigService urlconfigService;

	@RequestMapping("/update")
	public void update(HttpServletRequest request) throws Exception {
		List<Users> users = usersService.selectByExample(new UsersExample());
		for (Users u : users) {
			PinyinTool tool = new PinyinTool();
			Urlconfig uu = new Urlconfig();
			uu.setCreatetime(Produce.getNowDate());
			uu.setOemid(u.getId());
			uu.setPinyin(tool.toPinYin(u.getUsername(), "", Type.LOWERCASE));
			urlconfigService.insert(uu);
		}
	}

	@RequestMapping("/{name}")
	public ModelAndView downloadAPP(HttpServletRequest request, @PathVariable String name) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		if (name != null && name.equals("lanshugu")){
			String url = "redirect:http://smms.sbdznkj.com:2018/SbdVoip/download/" + name;
			return new ModelAndView(url);
		}else {
			UrlconfigExample urlconfigExample = new UrlconfigExample();
			urlconfigExample.getOredCriteria().add(urlconfigExample.createCriteria().andPinyinEqualTo(name));
			List<Urlconfig> agentUrl = urlconfigService.selectByExample(urlconfigExample);
			if (agentUrl == null || agentUrl.size() == 0) {
				modelAndView.setViewName("jsp/404");
				return modelAndView;
			} else {
				Users agent = usersService.selectByPrimaryKey(agentUrl.get(0).getOemid());
				modelAndView.setViewName("jsp/wap_download");
				if (agent.getUsername().equals("话淘")) {

					request.setAttribute("title", "话逍遥");
				} else {
					request.setAttribute("title", agent.getUsername());
				}
				request.setAttribute("bg", agent.getTheme());
				request.setAttribute("androidurl", agent.getAndroidpath());
				request.setAttribute("iosurl", agent.getIospath());
			}
		}
		return modelAndView;
	}
}
